package models;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import play.db.jpa.Model;

@Entity
public class Register extends Model {
	
	public int counter;
	public int penalty;
	public Date registrationDate;
	public byte classesToBeCompensated;
	
	public static final int COUNTER_INITIAL_VALUE = 10;
	public static final int TRINITY_PERIOD = 16;
		
	@ManyToOne 
	public Student student;
	
	@ManyToOne
	public Teacher teacher;
	
	@ManyToOne
	public Course course;
	
	public double amountToBePaid;
	
	@OneToMany (mappedBy = "registration", cascade = CascadeType.ALL)
	public List<Class> sClass;
	
	@OneToMany (mappedBy = "registration", cascade = CascadeType.ALL)
	public List<Payement> payments;

	public Register(Student student, Teacher teacher, Course course) {
		
		this.student = student;
		this.teacher = teacher;
		this.course = course;
		this.sClass = new LinkedList<Class>();
		this.payments = new LinkedList<Payement>();
		this.counter = this.COUNTER_INITIAL_VALUE;
		this.penalty = 0;
		this.registrationDate = new Date();
				
	}
	
	public static LinkedList<Date> getMonthDate(String date) {
    	
		LinkedList<Date> dates = new LinkedList<Date>();
		Date initial_date = null;
    	SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm");
		try {
			initial_date = formatter.parse(date);

		} catch (ParseException e) {
			
			e.printStackTrace();
			
		}
		int m = initial_date.getMonth();
		Date d = initial_date;
		
		while(d.getMonth()== m) {
			dates.add((Date)d.clone());
			d.setHours(d.getHours()+(24*7));
		}
		return dates;
		
    	
    }
	
public static LinkedList<Date> getTrinityDate(String date) {
    	
		LinkedList<Date> dates = new LinkedList<Date>();
		Date initial_date = null;
    	SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm");
		try {
			initial_date = formatter.parse(date);

		} catch (ParseException e) {
			
			e.printStackTrace();
			
		}
		int m = initial_date.getMonth();
		Date d = initial_date;
		
		for(int i=0 ; i<TRINITY_PERIOD ; i++) {
			System.out.println(d);
			dates.add((Date)d.clone());
			d.setHours(d.getHours()+(24*7));
		}
		return dates;
		
    	
    }
	
	public void addClass(Class c) {
		
		this.sClass.add(c);
		if(course.getType().equals("Basic"))
		this.amountToBePaid += course.price;
		else {
			if(this.amountToBePaid == 0) this.amountToBePaid = course.price;  
		}
		
	}
	
	
	public void resetClassesToBeCompensated(){
		
		int e = 0;
		int te = 0;
		
		for(int i = 0 ; i<sClass.size() ; i++) {
			
			Class c = sClass.get(i);
			
			if(c.student_attendance == c.EXCUSED) e=1;
						
			if((c.teacher_attendance == c.ABSCENT || 
				c.teacher_attendance == c.EXCUSED)
				&& c.student_attendance == c.ATTENDED) te++;
			
		}
		
		classesToBeCompensated = (byte) (e+te);
		
	}
	
	public Double getCPrice() {
		
		if(course.getType().equals("Trinity")) {
			
			return (course.price/Register.TRINITY_PERIOD);
		
		}
		else {
			
			return course.price;
		}
		
	}
	
		
	public static void main(String[] args) {
		
		getTrinityDate("09/08/2011 14:34");
		
	}
	
		

}
